package com.ykdl.member.kid.marketcard;

import java.util.ArrayList;

import net.wxxr.dataparse.model.DataParseError;
import net.wxxr.dataparse.model.DataState;
import net.wxxr.http.interfaces.ITag;
import net.wxxr.http.request.HttpBaseRequest;
import oauth.signpost.http.HttpParameters;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.lidroid.xutils.BitmapUtils;
import com.ykdl.member.constant.KidAction;
import com.ykdl.member.constant.KidConfig;
import com.ykdl.member.kid.R;
import com.ykdl.member.kid.beans.IntegralDetailsBean;
import com.ykdl.member.kid.beans.Listinfobean;
import com.ykdl.member.kid.beans.LotteryBean;
import com.ykdl.member.kid.beans.LotteryListBean;
import com.ykdl.member.kid.gears.BaseScreen;
import com.ykdl.member.kid.marketcard.IntegralDetailsActivity.getDataTag;
import com.ykdl.member.kid.util.AccessTokenKeeper;
import com.ykdl.member.kid.widget.AsyncImageView;
import com.ykdl.member.kid.widget.RefreshListView;
import com.ykdl.member.kid.widget.RefreshListView.OnRefreshListener;
import com.ykdl.net.manager.TaskManager;
import com.ykdl.net.manager.Wxxr;

public class IntegralIotteryActivity extends BaseScreen{
	private static AccessTokenKeeper mTokenKeeper;
	private RefreshListView listview_game ;
	private GameAdapter adapter;
	private BitmapUtils bitmapUtil ;
	private String point_type_id;
	private int cursor = 0;
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setTopTitle("积分游戏", 0, R.drawable.back_bg, new OnClickListener() {
			@Override
			public void onClick(View v) {
				
				finish();
			}
		}, 0, 0, null, R.drawable.title_bg, false);
		point_type_id = this.getIntent().getStringExtra("point_type_id");
		setContent(R.layout.integralgame_activity_xml);
		listview_game=(RefreshListView) findViewById(R.id.listview_game);
		listview_game.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				LotteryBean bean =(LotteryBean)adapter.getItem(position-1);
				Intent intent = new Intent(mContext, CouponinfoWebView.class);
				intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				intent.putExtra(KidAction.LOTTERY_ID, bean.getLottery_id());
				if (bean.getLottery_type()==1) {
					intent.putExtra(KidAction.URL_TYPE, 5);
				}else{
					intent.putExtra(KidAction.URL_TYPE, 6);
				}
				mContext.startActivity(intent);
			}
		});
		listview_game.setMoreClick(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					inint();
				}
			});
		listview_game.setonRefreshListener(new OnRefreshListener() {

			@Override
			public void onRefresh() {
				// TODO Auto-generated method stub
				cursor = 0;
				inint();
			}});
		inint();
	}
	public void inint() {
		showProgress("正在加载中,请稍等...");
		String  pageurl =KidConfig.LOTTERYLIST+point_type_id;
		HttpParameters parameterss = new HttpParameters();
		HttpBaseRequest request  =  Wxxr.getInstance().getRequest(pageurl,parameterss);
		 TaskManager.startHttpRequest(request, new getDataTag(),  LotteryListBean.class);
	}
	
	class getDataTag implements ITag {

		@Override
		public void refreshUI(Object t, DataState ds) {
			// TODO Auto-generated method stub
			finishProgress();
			if (t instanceof LotteryListBean) {
				LotteryListBean bean = (LotteryListBean)t;
				finishProgress();
				if (null==bean.getError()) {
					if (bean.getLottery().size()>0) {
						hidErrorTip( );
						listview_game.setVisibility(View.VISIBLE);
						adapter=new GameAdapter();
						adapter.setList(bean.getLottery());
						adapter.notifyDataSetChanged();
					}else {
						listview_game.setVisibility(View.GONE);
						showErrorTip("暂时没有内容哦","");
					}
					
				}else {
					Toast.makeText(mContext, "加载失败,"+bean.getDesc(), 1).show();
				}
				if (bean.getNext_cursor() == bean.getTotal_number()) {
					listview_game.setMoreButtoIsGon(true);
				} else {
					listview_game.setMoreButtoIsGon(false);
					cursor = bean.getNext_cursor();
				}
				listview_game.setAdapter(adapter);
				listview_game.onRefreshComplete();
				listview_game.setFecthMoreOk();
			}
		}

		@Override
		public boolean refreshError(DataParseError parseError) {
			// TODO Auto-generated method stub
			finishProgress();
			Toast.makeText(mContext, "加载失败", 1).show();
			return false;
		}

		@Override
		public void refreshCancel() {
			// TODO Auto-generated method stub
			
		}
		
	}
	
	class GameAdapter extends BaseAdapter{
		
		ArrayList<LotteryBean> list;  
		
		public void setList(ArrayList<LotteryBean> list) {
			this.list = list;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			if(list==null){
				return 0;
			}else{
				return list.size();
			}
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return list.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			ViewHolder holder=null;
			if(convertView==null){
				convertView = RelativeLayout.inflate(mContext,
						R.layout.gameadapter_item, null);
				holder =  new ViewHolder();
				holder.imageView1=(ImageView) convertView.findViewById(R.id.imageView1);
				holder.textView1=(TextView) convertView.findViewById(R.id.textView1);
				convertView.setTag(holder);
			}else{
				holder=(ViewHolder) convertView.getTag();
			}
			if(list.get(position)!=null){
				holder.textView1.setText(list.get(position).getName());
				if(list.get(position).getLottery_type()==1){
					holder.imageView1.setImageResource(R.drawable.scratch);
				}else if(list.get(position).getLottery_type()==2){
					holder.imageView1.setImageResource(R.drawable.turntable);
				}else{
					holder.imageView1.setImageResource(R.drawable.defualt_icon);
				}
			}	
			
			return convertView;
		}
		
	}
	
	class ViewHolder {
		ImageView imageView1;
		TextView textView1;
	}
	
}
